/**
 * 
 */
package mx.netbrain.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import mx.netbrain.controller.GroupsController;
import mx.netbrain.controller.OrganizationController;
import mx.netbrain.controller.PropertiesController;
import mx.netbrain.controller.RepositoriesController;
import mx.netbrain.controller.StorageController;
import mx.netbrain.controller.TypeFileController;
import mx.netbrain.converter.OrganizationConverter;
import mx.netbrain.converter.PropertiesUserConverter;
import mx.netbrain.converter.StorageConverter;
import mx.netbrain.dao.GroupUserDao;
import mx.netbrain.dao.PropertiesRepositoriesDao;
import mx.netbrain.dao.RepositoriesDao;
import mx.netbrain.dao.RepositoryGroupDao;
import mx.netbrain.dao.TreeDao;
import mx.netbrain.dao.TreeUserDao;
import mx.netbrain.dao.impl.GroupUserDaoImpl;
import mx.netbrain.dao.impl.PropertiesRepositoriesDaoImpl;
import mx.netbrain.dao.impl.RepositoriesDaoImpl;
import mx.netbrain.dao.impl.RepositoryGroupDaoImpl;
import mx.netbrain.dao.impl.TreeDaoImpl;
import mx.netbrain.dao.impl.TreeUserDaoImpl;
import mx.netbrain.model.Groups;
import mx.netbrain.model.Organization;
import mx.netbrain.model.PropertiesRepositories;
import mx.netbrain.model.Repositories;
import mx.netbrain.model.RepositoriesGroups;
import mx.netbrain.model.Storage;
import mx.netbrain.model.Tree;
import mx.netbrain.model.TreeUser;
import mx.netbrain.model.TreeUserId;
import mx.netbrain.model.TypeField;
import mx.netbrain.model.User;
import mx.netbrain.util.RepositoriesDefaultUtil;

import org.primefaces.event.TransferEvent;
import org.primefaces.model.DualListModel;

/**
 * @author julian
 * 
 */
public class RepositoriesBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3733307476163004776L;
	private String name;
	private String description;
	private Organization organization;
	private Groups group;
	private Storage storage;
	private List<TypeField> listType;
	private List<Storage> listStorage;
	private List<Organization> listOrganization;
	private DualListModel<Groups> listGroups;
	private List<Groups> source;
	private List<Groups> target;
	private List<PropertiesRepositories> listProperties;
	private List<Repositories> listRepositories;
	private Repositories repositories;
	private RepositoriesGroups repositoriesgroup;
	private TypeField typeField;
	private String label;
	private Integer size;
	private Boolean mandatory;

	public RepositoriesBean() {
		listOrganization = OrganizationConverter.organizationsDB;
		listType = PropertiesUserConverter.typesDB;
		listStorage = StorageConverter.storageDB;
	}

	public void prepareRepositorie(Long id) {
		RepositoriesDao repositoriesDao = new RepositoriesDaoImpl();
		repositories = repositoriesDao.findById(id);
	}

	public void prepareRepositorieGroup(Long id) {
		RepositoriesDao repoDao = new RepositoriesDaoImpl();
		repositories = repoDao.findById(id);
		RepositoryGroupDao repoGroupDao = new RepositoryGroupDaoImpl();
		source = new ArrayList<Groups>();
		target = new ArrayList<Groups>();
		source = repoGroupDao.findGroupNoRepository(repositories);
		target = repoGroupDao.findGroupRepository(repositories);
		listGroups = new DualListModel<Groups>(source, target);
	}

	public void save() {
		System.out.println(storage.getName());
		System.out.println(name);
		System.out.println(description);
		System.out.println(organization.getName());
		repositories = RepositoriesController.saveRepository(name, description,
				organization, storage);
		name = new String();
		description = new String();
	}

	public void addPropertiesDefaultNo() {
		RepositoriesDefaultUtil.savePropertiesDefault(repositories);
		repositories = new Repositories();
	}

	public void addPropertiesDefaultYes() {
		RepositoriesDefaultUtil.savePropertiesDefault(repositories);
		PropertiesRepositoriesDao propertiesRepositoriesDao = new PropertiesRepositoriesDaoImpl();
		listProperties = propertiesRepositoriesDao
				.listForRepositories(repositories);
	}

	public void addProperties() {
		PropertiesController.addProperties(label, mandatory, typeField,
				repositories, size);
		listProperties = PropertiesController.listForRepositories(repositories);
		label = new String();
		typeField = new TypeField();
		mandatory = null;
		size = null;
	}

	public void cancel() {
		repositories = new Repositories();
	}

	public void edit() {
		RepositoriesController.updateRepository(repositories);
	}

	public List<Organization> completeOrganization(String query) {
		return OrganizationController.completeOrganization(query,
				listOrganization);
	}

	public List<TypeField> completeType(String query) {
		return TypeFileController.completeType(query, listType);
	}

	public List<Storage> completeStorage(String query) {
		return StorageController.completeStorage(query, listStorage);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	public Groups getGroup() {
		return group;
	}

	public void setGroup(Groups group) {
		this.group = group;
	}

	public Storage getStorage() {
		return storage;
	}

	public void setStorage(Storage storage) {
		this.storage = storage;
	}

	public Repositories getRepositories() {
		return repositories;
	}

	public void setRepositories(Repositories repositories) {
		this.repositories = repositories;
	}

	public List<PropertiesRepositories> getListProperties() {
		return listProperties;
	}

	public void setListProperties(List<PropertiesRepositories> listProperties) {
		this.listProperties = listProperties;
	}

	public List<Repositories> getListRepositories() {
		RepositoriesDao repositoriesDao = new RepositoriesDaoImpl();
		listRepositories = repositoriesDao.listAll();
		return listRepositories;
	}

	public void setListRepositories(List<Repositories> listRepositories) {
		this.listRepositories = listRepositories;
	}

	public List<Storage> getListStorage() {
		return listStorage;
	}

	public void setListStorage(List<Storage> listStorage) {
		this.listStorage = listStorage;
	}

	public DualListModel<Groups> getListGroups() {
		if (listGroups == null) {
			listGroups = new DualListModel<>();
		}
		return listGroups;
	}

	public void setListGroups(DualListModel<Groups> listGroups) {
		this.listGroups = listGroups;
	}

	public TypeField getTypeField() {
		return typeField;
	}

	public void setTypeField(TypeField typeField) {
		this.typeField = typeField;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public Boolean getMandatory() {
		return mandatory;
	}

	public void setMandatory(Boolean mandatory) {
		this.mandatory = mandatory;
	}

	public void onTransfer(TransferEvent event) {
		for (Object item : event.getItems()) {
			RepositoryGroupDao repositoryGroupDao = new RepositoryGroupDaoImpl();
			repositoriesgroup = repositoryGroupDao.find(((Groups) item),
					repositories);
			if (repositoriesgroup != null) {
				// Se borra
				RepositoriesController.deleteRepository(((Groups) item),
						repositories);
			} else {
				// Se agrega
				if (GroupsController.validateGroup(((Groups) item),
						repositories)) {
					FacesMessage message = new FacesMessage(
							FacesMessage.SEVERITY_ERROR,
							"No se puede agregar el grupo: "
									+ ((Groups) item).getName()
									+ ", favor de revisar los usuarios del grupo",
							"");
					FacesContext.getCurrentInstance().addMessage(null, message);
				} else {
					repositoriesgroup = new RepositoriesGroups();
					RepositoriesController.saveRepositoriesGroups(
							((Groups) item), repositories);
					addTreeUser(((Groups) item));
					FacesMessage message = new FacesMessage(
							FacesMessage.SEVERITY_INFO, "El grupo: "
									+ ((Groups) item).getName()
									+ " se agrego correctamente.", "");
					FacesContext.getCurrentInstance().addMessage(null, message);
				}
			}
		}
	}

	public void addTreeUser(Groups groups) {
		List<User> listUserGroup = new ArrayList<User>();
		GroupUserDao groupUserDao = new GroupUserDaoImpl();
		listUserGroup = groupUserDao.findUserGroup(groups);
		TreeDao treeDao = new TreeDaoImpl();
		Tree tree = new Tree();
		tree = treeDao.findByRepository(repositories.getId());
		for (User userFor : listUserGroup) {
			TreeUserDao treeUserDao = new TreeUserDaoImpl();
			TreeUser treeUser = new TreeUser();
			TreeUserId treeUserId = new TreeUserId();
			treeUserId.setUser(userFor.getId());
			treeUserId.setProfile(groups.getProfile().getId());
			treeUserId.setTree(tree.getId());
			treeUser.setRepositories(repositories);
			treeUser.setId(treeUserId);
			treeUserDao.save(treeUser);
		}
	}

}
